home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / RTrace 1.0 / source / surface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-16  |  6.4 KB  |  162 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (c) 1988, 1992 Antonio Costa, INESC-Norte.
  3.  * All rights reserved.
  4.  *
  5.  * This code received contributions from the following people:
  6.  *
  7.  *  Roman Kuchkuda      - basic ray tracer
  8.  *  Mark VandeWettering - MTV ray tracer
  9.  *  Augusto Sousa       - overall, shading model
  10.  *
  11.  * Redistribution and use in source and binary forms are permitted
  12.  * provided that the above copyright notice and this paragraph are
  13.  * duplicated in all such forms and that any documentation,
  14.  * advertising materials, and other materials related to such
  15.  * distribution and use acknowledge that the software was developed
  16.  * by Antonio Costa, at INESC-Norte. The name of the author and
  17.  * INESC-Norte may not be used to endorse or promote products derived
  18.  * from this software without specific prior written permission.
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  */
  23. #include "defs.h"
  24. #include "extern.h"
  25.  
  26. /**********************************************************************
  27.  *    RAY TRACING - Surface - Version 7.3.1                           *
  28.  *                                                                    *
  29.  *    MADE BY    : Antonio Costa, INESC-Norte, October 1991           *
  30.  *    MODIFIED BY: Antonio Costa, INESC-Norte, July 1992              *
  31.  **********************************************************************/
  32.  
  33. /***** Surfaces *****/
  34. #define NEAR_ONE (0.95)
  35.  
  36. void
  37. get_surface_type1()
  38. {
  39.   real            value;
  40.   rgb_struct      smooth, opaque;
  41.  
  42.   ALLOCATE(surface[surfaces], surface_struct, 1);
  43.  
  44.   get_valid(scene, &value, 0.0, 1.0, "SURFACE COLOR Red");
  45.   surface[surfaces]->color.r = value;
  46.   get_valid(scene, &value, 0.0, 1.0, "SURFACE COLOR Green");
  47.   surface[surfaces]->color.g = value;
  48.   get_valid(scene, &value, 0.0, 1.0, "SURFACE COLOR Blue");
  49.   surface[surfaces]->color.b = value;
  50.  
  51.   get_valid(scene, &value, 0.0, 1.0, "SURFACE DIFFUSION Red");
  52.   surface[surfaces]->diffuse.r = value;
  53.   get_valid(scene, &value, 0.0, 1.0, "SURFACE DIFFUSION Green");
  54.   surface[surfaces]->diffuse.g = value;
  55.   get_valid(scene, &value, 0.0, 1.0, "SURFACE DIFFUSION Blue");
  56.   surface[surfaces]->diffuse.b = value;
  57.  
  58.   get_valid(scene, &value, 0.0, 1.0, "SURFACE SPECULARITY Red");
  59.   surface[surfaces]->specular.r = MIN(value, NEAR_ONE);
  60.   get_valid(scene, &value, 0.0, 1.0, "SURFACE SPECULARITY Green");
  61.   surface[surfaces]->specular.g = MIN(value, NEAR_ONE);
  62.   get_valid(scene, &value, 0.0, 1.0, "SURFACE SPECULARITY Blue");
  63.   surface[surfaces]->specular.b = MIN(value, NEAR_ONE);
  64.  
  65.   get_valid(scene, &value, 0.0, SPECULAR_FACTOR_MAX,
  66.             "SURFACE SPECULARITY PHONG Factor");
  67.   if (value < 3.0)
  68.     value = 3.0;
  69.   surface[surfaces]->phong_factor = value;
  70.  
  71.   get_valid(scene, &value, 0.0, 1.0, "SURFACE METALNESS Factor");
  72.   surface[surfaces]->metal_factor.r = value;
  73.   surface[surfaces]->metal_factor.g = value;
  74.   surface[surfaces]->metal_factor.b = value;
  75.  
  76.   get_valid(scene, &value, 0.0, 1.0, "SURFACE TRANSPARENCY Red");
  77.   surface[surfaces]->transparent.r = MIN(value, NEAR_ONE);
  78.   get_valid(scene, &value, 0.0, 1.0, "SURFACE TRANSPARENCY Green");
  79.   surface[surfaces]->transparent.g = MIN(value, NEAR_ONE);
  80.   get_valid(scene, &value, 0.0, 1.0, "SURFACE TRANSPARENCY Blue");
  81.   surface[surfaces]->transparent.b = MIN(value, NEAR_ONE);
  82.  
  83.   /* Compute remaining parameters */
  84.   opaque.r = 1.0 - surface[surfaces]->transparent.r;
  85.   opaque.g = 1.0 - surface[surfaces]->transparent.g;
  86.   opaque.b = 1.0 - surface[surfaces]->transparent.b;
  87.  
  88.   smooth.r = 1.0 - 3.0 / surface[surfaces]->phong_factor;
  89.   if (smooth.r < ROUNDOFF)
  90.     smooth.r = 0.0;
  91.   smooth.g = smooth.r;
  92.   smooth.b = smooth.r;
  93.  
  94.   surface[surfaces]->diffuse_factor.r = 1.0 - smooth.r *
  95.     surface[surfaces]->metal_factor.r;
  96.   surface[surfaces]->diffuse_factor.g = 1.0 - smooth.g *
  97.     surface[surfaces]->metal_factor.g;
  98.   surface[surfaces]->diffuse_factor.b = 1.0 - smooth.b *
  99.     surface[surfaces]->metal_factor.b;
  100. }
  101. #define PHONG_FACTOR (10000.0)
  102.  
  103. void
  104. get_surface_type2()
  105. {
  106.   real            value;
  107.   rgb_struct      smooth, opaque;
  108.  
  109.   ALLOCATE(surface[surfaces], surface_struct, 1);
  110.  
  111.   get_valid(scene, &value, 0.0, 1.0, "SURFACE COLOR Red");
  112.   surface[surfaces]->color.r = value;
  113.   get_valid(scene, &value, 0.0, 1.0, "SURFACE COLOR Green");
  114.   surface[surfaces]->color.g = value;
  115.   get_valid(scene, &value, 0.0, 1.0, "SURFACE COLOR Blue");
  116.   surface[surfaces]->color.b = value;
  117.  
  118.   get_valid(scene, &value, 0.0, 1.0, "SURFACE SMOOTHNESS Red");
  119.   smooth.r = value;
  120.   get_valid(scene, &value, 0.0, 1.0, "SURFACE SMOOTHNESS Green");
  121.   smooth.g = value;
  122.   get_valid(scene, &value, 0.0, 1.0, "SURFACE SMOOTHNESS Blue");
  123.   smooth.b = value;
  124.  
  125.   get_valid(scene, &value, 0.0, 1.0, "SURFACE METALNESS Red");
  126.   surface[surfaces]->metal_factor.r = value;
  127.   get_valid(scene, &value, 0.0, 1.0, "SURFACE METALNESS Green");
  128.   surface[surfaces]->metal_factor.g = value;
  129.   get_valid(scene, &value, 0.0, 1.0, "SURFACE METALNESS Blue");
  130.   surface[surfaces]->metal_factor.b = value;
  131.  
  132.   get_valid(scene, &value, 0.0, 1.0, "SURFACE TRANSPARENCY Red");
  133.   surface[surfaces]->transparent.r = MIN(value, NEAR_ONE);
  134.   get_valid(scene, &value, 0.0, 1.0, "SURFACE TRANSPARENCY Green");
  135.   surface[surfaces]->transparent.g = MIN(value, NEAR_ONE);
  136.   get_valid(scene, &value, 0.0, 1.0, "SURFACE TRANSPARENCY Blue");
  137.   surface[surfaces]->transparent.b = MIN(value, NEAR_ONE);
  138.  
  139.   /* Compute remaining parameters */
  140.   opaque.r = 1.0 - surface[surfaces]->transparent.r;
  141.   opaque.g = 1.0 - surface[surfaces]->transparent.g;
  142.   opaque.b = 1.0 - surface[surfaces]->transparent.b;
  143.  
  144.   surface[surfaces]->diffuse.r = (1.0 - POWER(smooth.r, 3.0)) * opaque.r;
  145.   surface[surfaces]->diffuse.g = (1.0 - POWER(smooth.g, 3.0)) * opaque.g;
  146.   surface[surfaces]->diffuse.b = (1.0 - POWER(smooth.b, 3.0)) * opaque.b;
  147.  
  148.   surface[surfaces]->specular.r = opaque.r - surface[surfaces]->diffuse.r;
  149.   surface[surfaces]->specular.g = opaque.g - surface[surfaces]->diffuse.g;
  150.   surface[surfaces]->specular.b = opaque.b - surface[surfaces]->diffuse.b;
  151.  
  152.   surface[surfaces]->phong_factor = 9.0 / (1.0 / PHONG_FACTOR + 3.0 -
  153.     smooth.r - smooth.g - smooth.b);
  154.  
  155.   surface[surfaces]->diffuse_factor.r = 1.0 - smooth.r *
  156.     surface[surfaces]->metal_factor.r;
  157.   surface[surfaces]->diffuse_factor.g = 1.0 - smooth.g *
  158.     surface[surfaces]->metal_factor.g;
  159.   surface[surfaces]->diffuse_factor.b = 1.0 - smooth.b *
  160.     surface[surfaces]->metal_factor.b;
  161. }
  162.